layui.use(['table', 'jquery', 'form', 'laydate'], function() {
	var table = layui.table;
	var $ = layui.jquery;
	var form = layui.form;
	var laydate = layui.laydate;

	table.render({
		elem: '#test',
		url: 'http://localhost:8080/springend-sys/pageListClassinfo',
		toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
		where: {
			method: "pageListClassinfo",
		},
		parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
			// console.log(res);
			return {
				"code": res.status, //解析接口状态
				"msg": res.message, //解析提示文本
				"count": res.data.count, //解析数据长度
				"data": res.data.classinfo, //解析数据列表
			};
		},
		defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
			title: '提示',
			layEvent: 'LAYTABLE_TIPS',
			icon: 'layui-icon-tips'
		}],
		title: '用户数据表',
		cols: [
			[{
				type: 'checkbox',
				fixed: 'left'
			}, {
				field: 'cid',
				title: 'ID',
				width: 60
			}, {
				field: 'cname',
				title: '班级名称',
			}, {
				field: 'num',
				title: '班级人数',
			}, {
				field: 'deptname',
				title: '系别',
			}, {
				field: 'tname',
				title: '老师姓名',
			}, {
				field: 'createtime',
				title: '建班时间',
			}, {
				field: 'overtime',
				title: '毕业时间',
			}, {
				fixed: 'right',
				title: '操作',
				toolbar: '#barDemo',
				width: 290
			}]
		],
		page: true,
	});
	//日期范围
	laydate.render({
		elem: '#test6'
			//设置开始日期、日期日期的 input 选择器
			//数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
			,
		range: ['#test-startDate-1', '#test-endDate-1']
	});
	//监听提交
	form.on('submit(demo1)', function(data) {
		//执行重载
		table.reload('test', {
			page: {
				curr: 1 //重新从第 1 页开始
			},
			where: {
				method: 'pageListClassinfo',
				cname: $("#Cname").val(),
				deptid: $("#Deptid").val(),
				tid: $("#Tname").val(),
				createtimeStart: $("#test-startDate-1").val(),
				createtimeEnd: $("#test-endDate-1").val(),
			}
		});
		return false;
	});
	//头工具栏事件
	table.on('toolbar(test)', function(obj) {
		var checkStatus = table.checkStatus(obj.config.id);
		switch (obj.event) {
			case 'addData':
				$("#classForm")[0].reset();
				layer.open({
					type: 1,
					title: '增加',
					area: '500px',
					//	offset: ['0%', '20%'],
					btn: ['确认', '取消'],
					content: $("#classForm"),
					yes: function(index, layero) {
						$.ajax({
							url: "http://localhost:8080/springend-sys/insertClassinfo",
							type: "post",
							data: {
								cname: $("#cname").val(),
								num: $("#num").val(),
								deptid: $("#deptid").val(),
								tname: $("#tname").val(),
								tid: $("#tid").val(),
								createtime: $("#createtime").val(),
								//	overtime: $("#overtime").val(),
								del: 0
							},
							dataType: "json",
							success: function(result) {
								layer.closeAll();
								layer.msg(result.message);
								$("#classForm")[0].reset();
								//执行重载
								table.reload('test');
							}
						})
					},
					btn2: function(index, layero) {}
				});
				break;
			case 'getCheckLength':
				layer.msg(checkStatus.isAll ? '全选' : '未全选');
				break;
		};
	});

	//触发行单击事件 row<单击> rowDouble<双击>
	// table.on('rowDouble(test)', function(obj) {
	// 	form.val("classForm", obj.data);
	// 	layer.open({
	// 		type: 1,
	// 		area: '500px',
	// 		offset: ['0%', '20%'],
	// 		title: '详情',
	// 		content: $("#classForm")
	// 	});
	// });
	//监听行工具事件
	table.on('tool(test)', function(obj) {
		var data = obj.data;
		//console.log(obj)
		if (obj.event === 'delData') {
			layer.confirm('真的删除行么', function(index) {
				$.ajax({
					url: "http://localhost:8080/springend-sys/deleteClassinfo",
					type: "post",
					data: {
						cid: data.cid
					},
					dataType: "json",
					success: function(result) {
						if (result.status == 0) {
							layer.closeAll();
							layer.msg(result.message);
							//执行重载
							table.reload('test');
						}
					}

				})
			});
		} else
		if (obj.event === 'edit') {
			form.val("classForm", data);
			layer.open({
				type: 1,
				title: '修改',
				area: '500px',
				//	offset: ['0%', '20%'],
				btn: ['确认', '取消'],
				content: $("#classForm"),
				yes: function(index, layero) {
					$.ajax({
						url: "http://localhost:8080/springend-sys/updateClassinfo",
						type: "post",
						data: {
							cid: $("#cid").val(),
							cname: $("#cname").val(),
							num: $("#num").val(),
							deptid: $("#deptid").val(),
							tname: $("#tname").val(),
							tid: $("#tid").val(),
							createtime: $("#createtime").val(),
							//	overtime: $("#overtime").val(),
							del: 0
						},
						dataType: "json",
						success: function(result) {
							if (result.status == 0) {
								layer.closeAll();
								layer.msg(result.message);
								$("#classForm")[0].reset();
								//执行重载
								table.reload('test');
							}
						}
					})
				},
				btn2: function(index, layero) {}
			});
		}
		if (obj.event === 'clsCourse') {
			form.val("classFormtwo", data);
			console.log(data);
			console.log($("#coname").val());
			layer.open({
				type: 1,
				title: '班级课程设定',
				area: '500px',
				//offset: ['0%', '20%'],
				btn: ['确认', '取消'],
				content: $("#classFormtwo"),
				yes: function(index, layero) {
					console.log($("#coname").val());
					console.log($("#cnames").val());
					$.ajax({
						url: "http://localhost:8080/springend-sys/insertCid",
						type: "post",
						data: {
							cid: data.cid,
							coid: $("#coname").val()
						},
						dataType: "json",
						success: function(result) {
							if (result.status == 0) {
								layer.closeAll();
								layer.msg(result.message);
								$("#classForm")[0].reset();
								//执行重载
								table.reload('test');
							}
						}
					})
				},
				btn2: function(index, layero) {

				},
			});
		}
		if (obj.event === 'TidclassCourseList') {
			// console.log(data);
			layer.open({
				type: 1,
				title: '班级课程查看',
				area: '800px',
				offset: ['30%', '14%'],
				content: $("#TidclassCourseListDiv"),
				success: function() {
					TidclassCourseListCid(data.cid);
				}
			});
		}
	});
	classFormtwoConame();

	function classFormtwoConame() {
		$.ajax({
			url: "http://localhost:8080/springend-sys/getAllclassFormtwoConame",
			type: "post",
			data: {},
			dataType: "json",
			success: function(result) {
				if (result.status == 0) {
					console.log(result.data[0].coid);
					$("#coname").html('');
					for (var i = 0; i < result.data.length; i++) {
						var option = '<option value="' + result.data[i].coid + '">' + result.data[i]
							.coname + '</option>';
						$("#coname").append(option);
						$("#Deptid").append(option);
					}
					form.render("select");
				}
			}
		})
	}
	//常规用法
	laydate.render({
		elem: '#createtime'
	});
});
